package com.cy.pj.brand.controller;

import com.cy.pj.brand.pojo.Brand;
import com.cy.pj.brand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
public class BrandController {

    @Autowired
    private BrandService brandService;

//    String name 为用户传入的信息, model 为 传给客户端的信息
/*传统方法:  /xxx/xxx/xx?name=TCL*/
/*  @GetMapping 注解中的{name}  可以中url连接获取中
获取url 中/localhost/brand/doFindBrands/TCL 对应位置的值, 即TCL
*   这种格式为 RESTful的技术, */
//    设定多个访问路径
    @RequestMapping(value={"/brand/doFindBrands","/brand/doFindBrands/{name}"})
//   为rest风格(一种软件架构风格)表达式, 通过/brand/diFindBrands/TCL 的形式传递数据(TCL为数据),
    /*rest格式语法:   /{a}/{b}/{c}/{d}, 变量需要用{}括起来*/
//    使用rest风格来传递数据, 需要在参数上添加注解, @PathVariable(),告诉框架数据来自url
//    @PathVariable 可以取得上面 大括号中的name(要求: 两者名字相同)
//    @PathVariable 中有个默认值, required = true 标识 这必须在url上填入值, 该参数表示name必须来自url
    public String doFindBrands(@PathVariable(required = false) String  name, Model model){
//        System.out.println(name);
        List<Brand> list = brandService.findBrands(name);
        model.addAttribute("list", list);
        return "brand/brand";
        /*前一个为brand目录, 后一个brand为HTML文件*/
    }

    @GetMapping("/brand/doDeleteBrandById/{id}")
    public String doDeleteBrandById(@PathVariable(required = false)Integer id, Model model){
        Integer rows = brandService.deleteBrandById(id);
        List<Brand> list = brandService.findBrands(null);
        model.addAttribute("list", list);
        return "/brand/brand";
    }

    @GetMapping("/brand/doAddUI")
    public String doAddUI(){
        return "brand/brand.add";
    }

    @PostMapping("/brand/doSaveBrand")
    public String doSaveBrand(Brand brand, Model model){
        System.out.println(brand);
//        存储数据
        int rows = brandService.insertBrand(brand);

      /*
        下面的代码在服务层实现, 控制层只控制业务的流程
        Assertions.assertEquals(1,rows);
        if(rows >= 1){
            System.out.println("添加成功,添加了 : " + rows + "行");
        }else{
            System.err.println("添加失败");
        }*/
//        存储完成后继续打印
        List<Brand> list = brandService.findBrands(null);
        model.addAttribute("list", list);
        return "brand/brand";
    }

    @GetMapping("/brand/doUpdateUI/{id}/{name}/{remark}")
    public String doUpdateUI(@PathVariable(required = false) Integer id,
                             @PathVariable(required = false)String name,
                             @PathVariable(required = false)String remark, Model model){
        Brand brand = new Brand();
        brand.setId(id);
        brand.setName(name);
        brand.setRemark(remark);
        List<Brand> list = new ArrayList<>();
        list.add(brand);
        model.addAttribute("list",list);
        System.err.println(id + name + remark);
        return "brand/brand.update";
    }
//
    @PostMapping("brand/doUpdateBrand")
    public String doUpdateBrand(Brand brand, Model model){
        System.out.println(brand);

        int rows = brandService.updateBrand(brand);

        List<Brand> list = brandService.findBrands(null);

        model.addAttribute("list", list);

        return "/brand/brand";
    }

    @GetMapping("brand/doFindBrandById/{id}")
    public String doUpdate(@PathVariable(required = false) Integer id, Model model){
        System.err.println(111111);
        Brand brand = brandService.findBrandById(id);
//        Brand brand = new Brand();
//        brand.setId(100);
//        brand.setName("只想赖着你");
//        brand.setRemark("我愿意为你忘记我姓名");
        System.err.println(brand);

        model.addAttribute("brand", brand);
        return "brand/brand.update1";

    }

}
